package com.wyl.salary.ui.page

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.wyl.salary.R
import com.wyl.salary.ui.theme.SalaryTheme
import com.wyl.salary.ui.viewmodel.DefaultSalaryViewModel

@Composable
fun DefaultSalaryPage(
    defaultSalary: String,
    onDefaultSalaryChange: (String) -> Unit,
    onSave: () -> Unit,
    onBackPress: () -> Unit,
) {
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        topBar = {
            TopAppBar(
                title = { Text("设置起止时间") },
                navigationIcon = {
                    IconButton(onClick = onBackPress) {
                        Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "返回")
                    }
                }, actions = {
                    IconButton(onClick = onSave) {
                        Icon(imageVector = Icons.Filled.Done, contentDescription = "完成")
                    }
                }
            )
        },
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            OutlinedTextField(
                value = defaultSalary,
                onValueChange = onDefaultSalaryChange,
                label = { Text(text = "默认时薪") },
                modifier = Modifier.fillMaxWidth(),
                placeholder = { Text(text = "请输入默认时薪") },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Number,
                    imeAction = ImeAction.Done,
                ),
                singleLine = true
            )
        }
    }
}

@Composable
fun DefaultSalaryPageWithViewModel(
    navController: NavHostController,
    viewModel: DefaultSalaryViewModel = viewModel(),
) {
    val context = LocalContext.current
    DefaultSalaryPage(
        defaultSalary = viewModel.defaultSalary,
        onDefaultSalaryChange = viewModel::onDefaultSalaryChanged,
        onSave = {
            if (viewModel.save()) {
                navController.popBackStack()
            } else {
                Toast.makeText(
                    context,
                    context.getString(R.string.number_error),
                    Toast.LENGTH_LONG
                ).show()
            }
        },
        onBackPress = navController::popBackStack
    )
}

@Preview
@Composable
private fun PreviewSetDurationPage() {
    SalaryTheme {
        DefaultSalaryPage(
            defaultSalary = "15.0",
            onDefaultSalaryChange = {},
            onSave = {},
            onBackPress = {}
        )
    }
}